// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: jhkim

#pragma once

#include "coin2/exchange/base/feed_util/trade.h"

struct BboContainer {
  void UpdateTrade(const Trade& trade) {
    if (!book_ready || !book_has_exchange_time) {
      return;
    }
    int64_t exchange_ts = trade.timestamp;
    switch (trade.side) {
      case coin2::exchange::feed::TRADE_BUY_SIDE:
        if (ask_time < exchange_ts) {
          ask_time = exchange_ts;
          ask_price = trade.price;
        }
        break;
      case coin2::exchange::feed::TRADE_SELL_SIDE:
        if (bid_time < exchange_ts) {
          bid_time = exchange_ts;
          bid_price = trade.price;
        }
        break;
      default:
        break;
    }
  }

  void UpdateBook(const IBook& book) {
    int64_t exchange_ts = book.Timestamp();
    book_ready = true;
    book_has_exchange_time = exchange_ts > 0;
    const auto& ask0 = book.Ask0();
    const auto& bid0 = book.Bid0();
    if (bid0 && (!book_has_exchange_time || bid_time <= exchange_ts)) {
      bid_time = exchange_ts;
      bid_price = bid0->price;
    }
    if (ask0 && (!book_has_exchange_time || ask_time <= exchange_ts)) {
      ask_time = exchange_ts;
      ask_price = ask0->price;
    }
  }

  bool book_ready = false;
  bool book_has_exchange_time = false;
  int64_t bid_time = 0L;
  int64_t ask_time = 0L;
  double bid_price = 0;
  double ask_price = 0;
};
